package com.zhang.sys;

import com.zhang.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * SysUserRole entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.zhang.sys.SysUserRole
 * @author MyEclipse Persistence Tools
 */

public class SysUserRoleDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(SysUserRoleDAO.class);
	// property constants
	public static final String ROLE_ID = "roleId";

	public void save(SysUserRole transientInstance) {
		log.debug("saving SysUserRole instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(SysUserRole persistentInstance) {
		log.debug("deleting SysUserRole instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public SysUserRole findById(com.zhang.sys.SysUserRoleId id) {
		log.debug("getting SysUserRole instance with id: " + id);
		try {
			SysUserRole instance = (SysUserRole) getSession().get(
					"com.zhang.sys.SysUserRole", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(SysUserRole instance) {
		log.debug("finding SysUserRole instance by example");
		try {
			List results = getSession().createCriteria(
					"com.zhang.sys.SysUserRole").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding SysUserRole instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from SysUserRole as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByRoleId(Object roleId) {
		return findByProperty(ROLE_ID, roleId);
	}

	public List findAll() {
		log.debug("finding all SysUserRole instances");
		try {
			String queryString = "from SysUserRole";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public SysUserRole merge(SysUserRole detachedInstance) {
		log.debug("merging SysUserRole instance");
		try {
			SysUserRole result = (SysUserRole) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(SysUserRole instance) {
		log.debug("attaching dirty SysUserRole instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SysUserRole instance) {
		log.debug("attaching clean SysUserRole instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public int countUserNotSysid(String userId,String sysId) {
		log.debug("finding all SysUserRole instances");
		try {
			String queryString = "select count(*) from SysUserRole kqxx where kqxx.id.userId=:userId and kqxx.id.sysId!=:sysId";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("userId", userId);
			queryObject.setParameter("sysId", sysId);
			return ((Number) queryObject.iterate().next()).intValue();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public int countUserSysid(String userId,String sysId) {
		log.debug("finding all SysUserRole instances");
		try {
			String queryString = "select count(*) from SysUserRole kqxx where kqxx.id.userId=:userId and kqxx.id.sysId=:sysId";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("userId", userId);
			queryObject.setParameter("sysId", sysId);
			return ((Number) queryObject.iterate().next()).intValue();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void deleteBySysId(String sysId) {
		log.debug("finding all SysUserRole instances");
		try {
			String queryString = "delete from SysUserRole kqxx where kqxx.id.sysId=:sysId and kqxx.id.userId!='admin'";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter("sysId", sysId);
			queryObject.executeUpdate();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public static void main(String[] args) {
		SysUserRoleDAO dao = new SysUserRoleDAO();
		System.out.println(dao.countUserSysid("admin", "1"));;
	}
}